using FluffyUnderware.Curvy;
using System;
using UnityEngine;
using static MovableManager;

public class MovableManager : MonoBehaviour
{
	[Serializable]
	public class MovableSet
	{
		public string setName = "Set";

		public MovableSplineAdapter[] movablePrefabs;

		public CurvySpline[] splines;

		public float minSpawnTime = 1f;

		public float maxSpawnTime = 3f;

		private float _spawnTime;

		private ObjectPool[] movablePools;

		public float SpawnTime
		{
			get
			{
				return _spawnTime;
			}
			set
			{
				_spawnTime = value;
			}
		}

		public void InitializePools()
		{
            if (movablePrefabs.Length > 0)
            {
				movablePools = new ObjectPool[movablePrefabs.Length];
				for (int i = 0; i < movablePrefabs.Length; i++)
				{
					ObjectPool objectPool = new GameObject($"Pool {movablePrefabs[i].name}").AddComponent<ObjectPool>();
					objectPool.prefab = movablePrefabs[i].gameObject;
					objectPool.initialSize = 4;
					objectPool.reallocationSize = 4;
					movablePools[i] = objectPool;
				}
			}
		}

		public MovableSplineAdapter GetRandomMovable()
		{
            if (movablePrefabs.Length > 0)
            {
				return movablePools[UnityEngine.Random.Range(0, movablePools.Length)].GetNext<MovableSplineAdapter>();
			}
			return null;
		}

		public CurvySpline GetRandomSpline()
		{
            if (movablePrefabs.Length > 0)
            {
				return splines[UnityEngine.Random.Range(0, splines.Length)];
			}
			return null;
		}

		public float GetNextSpawnTime()
		{
            if (movablePrefabs.Length > 0)
            {
				return UnityEngine.Random.Range(minSpawnTime, maxSpawnTime);
			}
			return 0f;
		}
	}

	[Serializable]
	public class EditorReferences
	{
		public MovableSet[] movableSets;
	}

	[SerializeField]
	public EditorReferences references = new EditorReferences();

	private void Start()
	{
		MovableSet[] movableSets = references.movableSets;
		foreach (MovableSet movableSet in movableSets)
		{
			movableSet.InitializePools();
			movableSet.SpawnTime = movableSet.minSpawnTime;
		}
		Prespawn();
		if ((bool)BaseGameManager.Instance && !BaseGameManager.Instance.SpawnInfiniteMovableElements)
		{
			base.enabled = false;
		}
	}

	private void Prespawn()
	{
		MovableSet[] movableSets = references.movableSets;
		foreach (MovableSet movableSet in movableSets)
		{
			if (movableSet.setName == "People")
			{
				CurvySpline[] splines = movableSet.splines;
				foreach (CurvySpline spline in splines)
				{
					int num = UnityEngine.Random.Range(0, 4);
					for (int k = 0; k < num; k++)
					{
						if(movableSet.movablePrefabs.Length > 0)
						{

						
						MovableSplineAdapter randomMovable = movableSet.GetRandomMovable();
						randomMovable.transform.position = new Vector3(1000f, 1000f, 1000f);
						randomMovable.SetInitialPosition(UnityEngine.Random.value);
						randomMovable.SetSpline(spline);
                        }
                    }
				}
			}
			else if (movableSet.setName == "Vehicles")
			{
				CurvySpline[] splines2 = movableSet.splines;
				foreach (CurvySpline spline2 in splines2)
				{
					if (movableSet.movablePrefabs.Length > 0)
					{
						MovableSplineAdapter randomMovable2 = movableSet.GetRandomMovable();
						randomMovable2.transform.position = new Vector3(1000f, 1000f, 1000f);
						randomMovable2.SetInitialPosition(UnityEngine.Random.value);
						randomMovable2.SetSpline(spline2);
					}
				}
			}
			else if (movableSet.setName == "Animals")
			{
				if (movableSet.movablePrefabs.Length > 0)
				{
					MovableSplineAdapter randomMovable3 = movableSet.GetRandomMovable();
					randomMovable3.transform.position = new Vector3(1000f, 1000f, 1000f);
					randomMovable3.SetInitialPosition(UnityEngine.Random.value);
					randomMovable3.SetSpline(movableSet.GetRandomSpline());
				}
			}
		}
	}

	private void Update()
	{
		if (!BaseGameManager.Instance || !BaseGameManager.Instance.HasGameStarted || BaseGameManager.Instance._hasGameEnded)
		{
			return;
		}
		MovableSet[] movableSets = references.movableSets;
		foreach (MovableSet movableSet in movableSets)
		{
			if (Time.time - movableSet.SpawnTime > 0f)
			{
				if (movableSet.movablePrefabs.Length > 0)
				{
					MovableSplineAdapter randomMovable = movableSet.GetRandomMovable();
					randomMovable.transform.position = new Vector3(1000f, 1000f, 1000f);
					randomMovable.SetInitialPosition(UnityEngine.Random.value);
					randomMovable.SetSpline(movableSet.GetRandomSpline());
					movableSet.SpawnTime = Time.time + movableSet.GetNextSpawnTime();
				}
			}
		}
	}
}
